/**	
 * Author : Mht
 * Desc : For 2016 RDM 
 */
package common;

import java.nio.charset.Charset;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;

/**
 * @author Mht
 * @date May 27, 2016
 */
public class HtmlUtil {

	public static HttpEntity<byte[]> responseXml(String result) {

		byte[] documentBody = null;
		if (StringUtils.isNotEmpty(result)) {
			result = translateSpecialXmlCharacter(result);
			documentBody = result.getBytes();
		} else {
			documentBody = StringUtils.EMPTY.getBytes();
		}
		Charset charset = Charset.forName("UTF-8");
		HttpHeaders header = new HttpHeaders();
		header.setContentType(new MediaType("text", "xml", charset));
		header.setContentLength(documentBody.length);
		return new HttpEntity<byte[]>(documentBody, header);
	}

	public static HttpEntity<byte[]> responseHtml(String result) {

		byte[] documentBody = null;
		if (StringUtils.isNotEmpty(result)) {
			result = translateSpecialXmlCharacter(result);
			documentBody = result.getBytes();
		} else {
			documentBody = StringUtils.EMPTY.getBytes();
		}
		Charset charset = Charset.forName("UTF-8");
		HttpHeaders header = new HttpHeaders();
		header.setContentType(new MediaType("text", "html", charset));
		header.setContentLength(documentBody.length);
		return new HttpEntity<byte[]>(documentBody, header);
	}

	private static String translateSpecialXmlCharacter(String xmlString) {
		if (StringUtils.isEmpty(xmlString)) {
			return StringUtils.EMPTY;
		}
		xmlString = xmlString.replace("&", "&amp;");
		return xmlString;
	}

}
